<?php

namespace rsa2;

class Rsa2 {

    /**
     * @desc 私钥
     * @var string
     */
    protected $privateKey = '';

    /**
     * @desc 公钥
     * @var string
     */
    protected $publicKey = '';

    /**
     * @desc 初始化公私钥
     * @param $privateKey
     * @param $publicKey
     */
    public function __construct($privateKey, $publicKey) {
        $this->privateKey = $privateKey;
        $this->publicKey = $publicKey;
    }

    /**
     * @desc 设置私钥
     * @param $privateKey
     */
    protected function setPrivateKey($privateKey) {
        $this->privateKey = $privateKey;
    }

    /**
     * @desc 设置私钥
     * @param $publicKey
     */
    protected function setPublicKey($publicKey) {
        $this->publicKey = $publicKey;
    }

    /**
     * 获取私钥
     * @return bool|resource
     */
    public function getPrivateKey() {
        return openssl_pkey_get_private($this->privateKey);
    }

    /**
     * 获取公钥
     * @return bool|resource
     */
    public function getPublicKey() {
        return openssl_pkey_get_public($this->publicKey);
    }

    /**
     * 创建签名
     * @param string $data 数据
     * @return bool|string
     */
    public function createSign($data = '') {
        if (!is_string($data)) {
            return false;
        }
        return openssl_sign($data, $sign, $this->getPrivateKey(), OPENSSL_ALGO_SHA256) ? base64_encode($sign) : false;
    }

    /**
     * 验证签名
     * @param string $data 数据
     * @param string $sign 签名
     * @return bool
     */
    public function verifySign($data = '', $sign = '') {
        if (!is_string($sign) || !is_string($sign)) {
            return false;
        }
        return (bool) openssl_verify(
                        $data, base64_decode($sign), $this->getPublicKey(), OPENSSL_ALGO_SHA256
        );
    }

    /**
     * @desc 将字符串格式公私钥格式化为pem格式公私钥
     * @param string $secretKey 公钥和私钥
     * @param string $type 公私钥
     * @return string
     */
    public  function formatSecretKey($secretKey, $type = 'private') {
        // 64个英文字符后接换行符"\n",最后再接换行符"\n"
        $key = (wordwrap($secretKey, 64, "\n", true))."\n";
        // 添加pem格式头和尾
        if ($type == 'public') {
            $pem_key = "-----BEGIN PUBLIC KEY-----\n" . $key . "-----END PUBLIC KEY-----\n";
        }else if ($type == 'private') {
            $pem_key = "-----BEGIN RSA PRIVATE KEY-----\n" . $key . "-----END RSA PRIVATE KEY-----\n";
        }else{
            return false;
        }
        return $pem_key;
    }

    /**
     * @desc 将JAVA密钥替换为非JAVA适用头部和尾部
     * @param string $privateKey
     * @return string
     */
    public function replacePrivateKey($privateKey) {
        $beforeReplace = ['-----BEGIN PRIVATE KEY-----', '-----END PRIVATE KEY-----'];
        $afterReplace = ['-----BEGIN RSA PRIVATE KEY-----', '-----END RSA PRIVATE KEY-----'];
        return str_replace($beforeReplace, $afterReplace, $privateKey);
    }

    /**
     * 创建签名
     * @param string $data 数据
     * @return bool|string
     */
    public function createSignJava($data = '') {
        if (!is_string($data)) {
            return false;
        }
        return openssl_sign($data, $sign, $this->getPrivateKey(), OPENSSL_ALGO_DSS1) ? base64_encode($sign) : false;
    }

    /**
     * 验证签名
     * @param string $data 数据
     * @param string $sign 签名
     * @return bool
     */
    public function verifySignJava($data = '', $sign = '') {
        if (!is_string($sign) || !is_string($sign)) {
            return false;
        }
        return (bool) openssl_verify(
                        $data, base64_decode($sign), $this->getPublicKey(), OPENSSL_ALGO_DSS1
        );
    }

}
